﻿using System;
using System.ComponentModel;
using System.Data;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.Task.Context
{
    public class Client : Crud
    {
        #region Protected Overridable Function
        protected override void BgwFetchListsDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (PagingLists)e.Argument;

            try
            {
                e.Result = new model.Table.Client().GetClientLists(a.Page, a.Size, a.FilterString);
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = new DataTable();
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }   
        }

        protected override void BgwInsertDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (DataRow)e.Argument;

            try
            {
                var lastName = a["last_name"].ToString();
                var firstName = a["first_name"].ToString();
                var middleName = a["middle_name"].ToString();
                var gender = a["gender"].ToString();
                var address = a["address"].ToString();
                var birthDate = Convert.ToDateTime(a["birth_date"].ToString());
                var email = a["email"].ToString();
                var mobileNum = a["mobile_num"].ToString();
                var telNum = a["tel_num"].ToString();
                var userCreateId = ToUInt64(a["user_create_id"].ToString());

                a["id"] = new model.Table.Client().Insert(lastName, firstName, middleName, gender, address, birthDate, email, mobileNum, telNum, userCreateId);
                
                if (!ToUInt64(a["id"].ToString()).Equals(0))
                    a["created_date"] = DateTime.Now;

                e.Result = a;
            }
            catch (Exception ex)
            {
                // Error Logs here
                a["id"] = 0;
                e.Result = a;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }
        }

        protected override void BgwUpdateDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (DataRow)e.Argument;
            try
            {
                var id = ToUInt64(a["id"].ToString());
                var lastName = a["last_name"].ToString();
                var firstName = a["first_name"].ToString();
                var middleName = a["middle_name"].ToString();
                var gender = a["gender"].ToString();
                var address = a["address"].ToString();
                var birthDate = Convert.ToDateTime(a["birth_date"].ToString());
                var email = a["email"].ToString();
                var mobileNum = a["mobile_num"].ToString();
                var telNum = a["tel_num"].ToString();
                //var clientNo = a["client_no"].ToString();
                var userCreateId = ToUInt64(a["user_create_id"].ToString());
                IsSuccess = new model.Table.Client().Update(id, lastName, firstName, middleName, gender, address, birthDate, email, mobileNum, telNum, userCreateId);
                e.Result = a;
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = a;
                IsSuccess = false;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }
        }

        protected override void BgwDeleteDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var a = (DataRow)e.Argument;
                IsSuccess = new model.Table.Client().Delete(ToUInt64(a["id"].ToString()));
                e.Result = a;
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = 0;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }      
        }

        protected override void BgwGetCountDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                e.Result = new model.Table.Client().GetCount();
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = 0;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }
        }

        protected override void BgwGetCountWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TriggerGetCount(new FormEvent(!IsError) { Value = e.Result });
            IsError = false;
        }
        #endregion
    }
}
